<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ page import="com.hzqy.web.vo.PmsUserVo" %>
<%@ page import="com.hzqy.commons.utils.ConstantUtils" %>
<%@ page import="com.hzqy.commons.utils.SystemConfigUtils" %>
<%@ taglib prefix="limits" uri="/WEB-INF/tld/limits.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户选择</title>
<link rel="stylesheet" type="text/css"	href="../../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"	href="../../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"	href="../../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"	href="../../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"	href="../../static/js/themes/icon.css">
<link href="../../static/css/uploadify.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript" src="../../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../static/js/index.js"></script>
<script src="../../static/js/jquery.uploadify.js" type="text/javascript"></script>
<%
	String idField =request.getParameter("idField");
	String nameField =request.getParameter("nameField");
	
	PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
	String designatedOrgs = SystemConfigUtils.getSysConfig("OnDemandTotalTimeStatisticsJson_"+userVo.getOrgId());
	boolean isInit = true;
	if(designatedOrgs!=null && !"".equals(designatedOrgs)){
		isInit = false;
	}
	
%>

<script type="text/javascript">
		var queryParams;
		function EasyuiDatagrid() {
			$("#easyui-datagrid").datagrid({
				url: '../../pms/industry_orgVo_list.do',
				nowrap: true,
				striped: true,
				border: false,
				fit: true,
				rownumbers:true,
				queryParams: getGueryParams(),
				columns: [[
					{field:'f_po_id',title:'客户ID',align:'center',width:100},
					{field:'f_po_name',title:'客户名称',align:'center',width:650}
				]],
				singleSelect:true,
				pagination:true,
				pageList:[10,20,30,40,50],
				pageSize:10,
				toolbar: '#toolbar',
				onExpandRow: function(index,row){
					
				},
				onSelect: function(rowIndex,rowData) {
					
				},
				onUnselect: function(rowIndex,rowData) {
					
				},
				onCheckAll: function(rowIndex,rowData) {
					
				},
				onUncheckAll: function(rowIndex,rowData) {
				
				},
				onLoadSuccess: function(data) {
					if(data.total==-1) {
						$.messager.defaults={ok:"确定",cancel:"取消"};
						$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
						if (r){
						    window.parent.location.href='/login.do';
						    return false;
						}});
					}
				}
			});
		}
		
		function getGueryParams(){
			return {
				f_po_name:$("#f_po_name").val(),
				f_po_status:$("#f_po_status").val()
			};
		}
		
		var dataArray=[];
		var searchDataArray=[];
		$(function(){
			if(<%=isInit%>){
				new EasyuiDatagrid();
				gotoSearch = function(){
					$("#easyui-datagrid").datagrid('reload',getGueryParams());
				};
			}else{
				//初始化
				var designatedOrgs="<%=designatedOrgs%>";
				var designatedOrgsArray = designatedOrgs.split(",");
				for(var i=0;i<designatedOrgsArray.length;i++){
					var dataJson={};
					var scatteredJson = designatedOrgsArray[i].split("_");
					dataJson["f_po_id"] = scatteredJson[0];
					dataJson["f_po_name"] = scatteredJson[1];
					dataArray.push(dataJson);
				}
				
				loadTable();
				loadTableData();
				
				//点击搜索时
				gotoSearch = function(){
					var fPoName = $("#f_po_name").val();
					if(fPoName==null||fPoName==""){
						loadTable();
						loadTableData();
					}else{
						
						for(var i in dataArray){//遍历json数组时，这么写i为索引，0,1
							 
							var poName=dataArray[i].f_po_name;
							
							if(poName.indexOf(fPoName) != -1){
								searchDataArray.push(dataArray[i]);
							}
							
						}
						
						loadSearchTableData();
						
					}
					
				};
				
			}
			
		});
		
		function loadTable(){
			$("#easyui-datagrid").datagrid({
				nowrap: true,
				striped: true,
				border: false,
				fit: true,
				rownumbers:true,
				columns: [[
					{field:'f_po_id',title:'客户ID',align:'center',width:100},
					{field:'f_po_name',title:'客户名称',align:'center',width:650}
				]],
				singleSelect:true,
				pagination:true,
				pageList:[10,20,30,40,50],
				pageSize:10,
				toolbar: '#toolbar',
				onExpandRow: function(index,row){
					
				},
				onSelect: function(rowIndex,rowData) {
					
				},
				onUnselect: function(rowIndex,rowData) {
					
				},
				onCheckAll: function(rowIndex,rowData) {
					
				},
				onUncheckAll: function(rowIndex,rowData) {
				
				},
				onLoadSuccess: function(data) {
					if(data.total==-1) {
						$.messager.defaults={ok:"确定",cancel:"取消"};
						$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
						if (r){
						    window.parent.location.href='/login.do';
						    return false;
						}});
					}
				}
			});
		}
		
		function loadTableData(){
			$("#easyui-datagrid").datagrid('loadData', dataArray); //将数据绑定到datagrid 
		}
		
		function loadSearchTableData(){
			$("#easyui-datagrid").datagrid('loadData', searchDataArray); //将数据绑定到datagrid 
		}
		
		
		function updateOrg(){
			var rowInfo = $("#easyui-datagrid").datagrid('getSelected');
			if(rowInfo==null){
				$.messager.alert('提示', "您未选择客户", 'info', function() {
					$.messager.progress('close');
				});
				return;	
			}
			window.parent.document.getElementById("<%=nameField%>").value=rowInfo.f_po_name;
			window.parent.document.getElementById("<%=idField%>").value=rowInfo.f_po_id;
			parent.$('#org_list').dialog('close')
		}
		function onpress(){
			var event=arguments.callee.caller.arguments[0]||window.event;//消除浏览器差异
	   		  	if (event.keyCode == 13){  
	      			gotoSearch($('#easyui-treegrid'));
	    		}
		}
</script>
</head>
<body>
	<input type="hidden" id="sessionId" value="${pageContext.session.id}" />
	<table id="easyui-datagrid" title="客户列表"></table>
	<div id="toolbar" class="easyui_datagrid_toolbar" style="">
		<div class="easyuiSearch">
			<form onsubmit="return false;" method="post">
				<table cellspacing="0" cellpadding="0" width="90%">
					<tr>
						<td align="left" width="5%">客户名称：</td>
						<td align="left" width="10%"><input name="f_po_name" id="f_po_name" style="width: 150px;"  onkeydown="onpress();"/></td>
						<input type="hidden" id="f_po_status" name="f_po_status"  value="1"/>
						<td align="left" width="10%">
							<limits:uiAuth url="org_list.do">
								<a href="javascript:void(0);" class="easyui-linkbutton"
									data-options="iconCls:'icon-search', plain:true"
									onclick="gotoSearch($('#easyui-datagrid'))">查询</a>
							</limits:uiAuth>
						</td>
						<td align="right" width="10%">
							<a id="easyui-linkbutton" class="easyui-linkbutton"
							data-options="iconCls:'icon-ok'" href="javascript:void(0);"
							onclick="updateOrg()">确认</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</div>		
</body>
</html>